<?php
include("../../tools/session.php");
include("../../tools/controls.php");
include("../../tools/pub.php");

// 如果登录失败、无权限访问，那么采取相应措施
if (!($is_login_success === true)) { session_hop_page("../index.php"); exit(); }
if (!power_check(0)) { include("tpl-inc-wp.php"); $tpl->display($oa_tpl_path . "/no-power.html"); exit(); }

/*-- 以下是实际内容 --*/
$page_status = $_POST["page_status"];
$author = $_SESSION["user"]["username"];

if (!$page_status)
{
    include("tpl-inc-wp.php");
    $tpl->display($oa_tpl_path . "/default/doc_template.html");
}
elseif ($page_status == 1)
{
    include("../../tools/mysql.php");
    db_connect();
    
    $tpl_class_id		= $_POST["tpl_class_id"];
    $type				= $_POST["type"];
    $tpl					= mysql_escape_string($_POST["tpl"]);
    $name				= $_POST["name"];
    $pic					= $_POST["pic"];
    $tpl_pars			= mysql_escape_string($_POST["tpl_pars"]);
    
    // $sql = "INSERT INTO doc_tpl(tpl_class_id, name, template, type, pic, author, create_time) ";
    // $sql .= " VALUES(" . $tpl_class_id . ", '" . $name . "', '" . $tpl . "', '" . $type . "', '" . $pic . "', '" . $author . "', NOW())";
    // echo $sql;
    $sql = "INSERT INTO doc_tpl(tpl_class_id, name, template, pars, type, pic, author, create_time) 
    		VALUES(" . $tpl_class_id . ", '" . $name . "', '" . $tpl . "', '" . $tpl_pars . "', '"
    		. $type . "', '" . $pic . "', '" . $author . "', NOW())";
    $log_str = array(mysql_escape_string("创建 ID=" . $tpl_class_id . " 类的“" . (($type == 1) ? "表单" : "HTML") . "填写”型新公文模板“" . $name . "”成功！"),
    				mysql_escape_string("创建新公文模板失败！"));
    echo check_status_write_to_log(db_exec($sql), $log_str, FALSE);
     
}
elseif ($page_status == 2)
{
    include("../../tools/mysql.php");
    db_connect();
    
    header ("content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    echo "<classes>\n";
    
    $sql = "SELECT tpl_class_id, name, zh_name FROM doc_tpl_class ORDER BY sequence ASC";
    $re = db_query($sql);
    
    foreach ($re as $v)
    {
		echo "<item id=\"" . $v["tpl_class_id"] . "\" name=\"" . $v["name"] . "\" zhname=\"" . $v["zh_name"] . "\" />\n";
    }
    
    echo "</classes>\n";
}
?>
